/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveCommands
{
	class GrooveCreateEventSubscription : CommandUtils.GrooveUtilBase
	{
		private string m_EventClass;
		private string m_SourceURI;
		private string m_CallbackURI;
		private string m_Query;
		private string m_SubscriptionID;

		private int m_TimeToLive;

		[STAThread]
		static void Main(string[] i_Arguments)
		{
			CommandUtils.UtilAppMain.RunApplication(new GrooveCreateEventSubscription(i_Arguments));
		}

		public GrooveCreateEventSubscription(string[] i_Arguments)
			: base (i_Arguments)
		{
			base.UsageMessage = "GrooveCreateEventSubscription [PARAMS...]";
			base.HelpMessage = 
				"GrooveCreateEventSubscription\r\n\t" + 
				"Creates an event subscription\r\n" +
				"Parameters:\r\n\t" +
				"/EventClass=         Event class of the event source\r\n\t" +
				"/SourceURI=          The Web Services URI of the event source\r\n\t" +
				"/CallbackURI=        The Web Services URI of the event queue\r\n\t" +
				"/TimeToLive=         Time (in hours) for subscription to live\r\n\t" +
				"[/Query=             Event filter for aggregated events]\r\n\t" +
				"[/SubscriptionID=    ID of subscription to renew]\r\n\t" +
				"[/Account=           The Web Services URI for the Groove account]\r\n\t" +
				"[/Identity=          The Web Services URI for the Groove identity]\r\n\t" +
				"[/RequestKey=        Token used to communicate Groove host]\r\n\t" +
				"[/Host=              The remote Groove host name to issue commands to]\r\n\t" +
				"[/Port=              The remote Groove host port to issue commands to]\r\n\t" +
				"[/Timeout=           Timeout (in ms) for Web Services method execution]\r\n\t" +
				"[/X=                 Configuration file to use for parameter input]\r\n\t" +
				"\r\n";
		}

		public override CheckResult CheckParameters()
		{
			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded )
				return CheckResult;

			m_EventClass = base.GetParameterValue("EventClass");
			if (System.String.IsNullOrEmpty(m_EventClass))
			{
				base.DisplayUsage("You must specify an event class.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_SourceURI = base.GetParameterValue("SourceURI");
			if (System.String.IsNullOrEmpty(m_SourceURI))
			{
				base.DisplayUsage("You must specify a source uri.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_CallbackURI = base.GetParameterValue("CallbackURI");
			if (System.String.IsNullOrEmpty(m_CallbackURI))
			{
				base.DisplayUsage("You must specify a callback uri.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_TimeToLive = base.GetParameterValueInt("TimeToLive", 0);
			if (m_TimeToLive == 0)
			{
				base.DisplayUsage("You must specify a non-zero time to live.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_Query = base.GetParameterValue("Query");

			m_SubscriptionID = base.GetParameterValue("SubscriptionID");

			return base.CheckUnusedParameters();
		}

		public override CommandUtils.UtilBase.CheckResult Run()
		{
			GrooveWebServicesV12Helpers.Subscription Subscription = new GrooveWebServicesV12Helpers.Subscription
				(
				base.Context,
				base.IdentityURI,
				m_EventClass,
				m_Query,
				m_SourceURI,
				m_CallbackURI,
				m_SubscriptionID,
				m_TimeToLive,
				true			//  Start subscription
				);

			base.StandardOutput.WriteLine("<Subscription ID='" + Subscription.SubscriptionID + "'/>");

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}
	}
}
